//
//  DianpingCell.m
//  diaoyuren
//
//  Created by 钓鱼人 on 16/3/3.
//  Copyright © 2016年 game. All rights reserved.
//

#import "DianpingCell.h"
#import "ZanButton.h"
#import "TQStarRatingView.h"
#import "SDPhotoGroup.h"
#import "SDPhotoBrowser.h"
#import "IslanDescribImageModel.h"
#import "UIButton+WebCache.h"
#define imageXX 15
#define imageXd 8
#define imageHight ((SCREEN_WIDTH-imageXd*2-imageXX*2)/3)

@interface DianpingCell ()<SDPhotoBrowserDelegate>

@property (nonatomic,strong) SDPhotoBrowser *browser;
/**
 *  头像
 */
@property (weak, nonatomic) IBOutlet UIButton *Imghead;
/**
 * 头像 高 宽
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgheight;
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *imgwidth;
/**
 * 用户名
 */
@property (weak, nonatomic) IBOutlet UILabel *userName;
/**
 *  等级
 */
@property (weak, nonatomic) IBOutlet UILabel *grad;
/**
 * 等级宽度
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *gradwidth;

/**
 * 分数
 */
@property (weak, nonatomic) IBOutlet UILabel *scroe;
/**
 *  评论
 */
@property (weak, nonatomic) IBOutlet UILabel *comment;

/**
 *  分数Lab中心X 距离 Cell中心X的偏移量
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *scroeXD;

/**
 *  时间
 */
@property (weak, nonatomic) IBOutlet UILabel *time;
/**
 *  底部线高
 */
@property (weak, nonatomic) IBOutlet NSLayoutConstraint *linHight;

@property (nonatomic,strong) TQStarRatingView *starRating;

/**
 *  承载图片的底视图
 */
@property (weak, nonatomic) IBOutlet UIView *imageBack;
@property (nonatomic,strong) AppDelegate *app;

/**
 *  点赞btn
 */
@property (weak, nonatomic) IBOutlet UIButton *primisBtn;

@end

@implementation DianpingCell

- (void)awakeFromNib {
    
    self.app = (AppDelegate *)[UIApplication sharedApplication].delegate;
    
    self.selectionStyle = 0;
    
    self.contentView.backgroundColor = [UIColor whiteColor];
    
    self.linHight.constant = 0.5;
    
    self.imgheight.constant = SCREEN_HEIGHT*0.063;
    self.imgwidth.constant  = SCREEN_HEIGHT*0.063;
    
    radius(self.Imghead, (SCREEN_HEIGHT*0.063)/2);
    
    self.userName.font = font(SCREEN_WIDTH*0.04);
   
    self.grad.font = font(SCREEN_WIDTH*0.029);
    self.gradwidth.constant = 25;
    
    self.scroe.font = font(SCREEN_WIDTH*0.03);
    
    self.comment.font = font(SCREEN_WIDTH*0.043);
    self.comment.numberOfLines = 0;
    
    self.time.font = font(SCREEN_WIDTH*0.03);
    
    
    /**
     *  添加 星星评分
     */
    CGFloat  ww = SCREEN_HEIGHT*0.14;
    CGFloat hh  = SCREEN_HEIGHT*0.023;
    CGFloat yy = (SCREEN_HEIGHT*0.115-hh)/2;
    CGFloat xx = SCREEN_HEIGHT*0.1;
    
    self.starRating = [[TQStarRatingView alloc]initWithFrame:CGRectMake(xx, yy, ww, hh)];
    [self addSubview:self.starRating];
   
    self.starRating.userInteractionEnabled = YES;
    
    self.scroeXD.constant =  -ww/4+10;
    
    self.userName.userInteractionEnabled = YES;
    
    UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc]initWithTarget:self action:@selector(personSpaceClick)];
    [self.userName addGestureRecognizer:tap];
    
    [self.Imghead addTarget:self action:@selector(personSpaceClick) forControlEvents:UIControlEventTouchUpInside];
}

-(void)setModel:(IslandCommentModel *)model{
    
    _model = model;
    
    [self.starRating StarForegroundViewWithScore:_model.score];
    [self.Imghead sd_setImageWithURL:[NSURL URLWithString:_model.avatar] forState:UIControlStateNormal];
    self.userName.text= _model.username;
    self.grad.text = [NSString stringWithFormat:@"Lv.%@",_model.user_level];
    self.comment.text  = _model.comment;
    self.time.text = _model.addtime;
    
    CGSize size = CGSizeMake(SCREEN_WIDTH-30, 0);
    
    self.scroe.text = [NSString stringWithFormat:@"%@分",_model.score];
    
    if (_model.iszan) {
        self.primisBtn.selected = YES;
    }else{
        self.primisBtn.selected = NO;
    }
    
    if (_model.imgs.count==0) {
         self.CellHight = [Manager CountTextWhithSize:size AndString:self.comment.text AndfFontSize:SCREEN_WIDTH*0.05]+100;
        
    }else if (_model.imgs.count<=3) {
        
        self.CellHight = [Manager CountTextWhithSize:size AndString:self.comment.text AndfFontSize:SCREEN_WIDTH*0.05]+110+imageHight;
    }else{
    
        self.CellHight = [Manager CountTextWhithSize:size AndString:self.comment.text AndfFontSize:SCREEN_WIDTH*0.05]+110+(imageHight)*2;
    }
    
     [self.primisBtn setImage:[UIImage imageNamed:@"btn-reply-green"] forState:UIControlStateSelected | UIControlStateHighlighted];
    
}

-(void)drawRect:(CGRect)rect{
    
    [self.imageBack.subviews makeObjectsPerformSelector:@selector(removeFromSuperview)];
    
    NSInteger count = self.model.imgs.count;
    
    if (count>6) {
        count=6;
    }
    
    for (int i=0; i<self.model.imgs.count; i++) {
        
        UIButton *btn = [UIButton buttonWithType:UIButtonTypeCustom];
        
        IslanDescribImageModel *imageModel = self.model.imgs[i];
        
        [btn sd_setImageWithURL:[NSURL URLWithString:imageModel.small_url] forState:UIControlStateNormal];
        
        NSInteger row = i%3;
        NSInteger los = i/3;

        CGFloat yd = 8;
        
        btn.frame = CGRectMake(imageXX+(imageXd+imageHight)*row, (yd+imageHight)*los, imageHight, imageHight);
        btn.tag = i+100;
        btn.imageView.contentMode  = UIViewContentModeScaleAspectFill;
        
        [self.imageBack addSubview:btn];
        
        [btn addTarget:self action:@selector(ShowImageClick:) forControlEvents:UIControlEventTouchUpInside];
    }
}

-(void)ShowImageClick:(UIButton *)btn{
    _browser = [[SDPhotoBrowser alloc] init];
    _browser.sourceImagesContainerView = self.imageBack; // 原图的父控件
    _browser.imageCount = _model.imgs.count; // 图片总数
    _browser.delegate = self;
    
    _browser.alpha = 0;
    _browser.currentImageIndex = (int)btn.tag-100;
    
    [_browser show];
    
    [UIView animateWithDuration:0.8 animations:^{
        _browser.alpha = 1;
    }];
}

- (UIImage *)photoBrowser:(SDPhotoBrowser *)browser placeholderImageForIndex:(NSInteger)index
{
    return nil;
}

- (NSURL *)photoBrowser:(SDPhotoBrowser *)browser highQualityImageURLForIndex:(NSInteger)index
{
    IslanDescribImageModel *model = self.model.imgs[index];
    NSString *urlStr = [model.big_url stringByReplacingOccurrencesOfString:@"thumbnail" withString:@"bmiddle"];
    return [NSURL URLWithString:urlStr];
}

- (IBAction)primis:(UIButton *)sender {
        
    if ([self.app.isLogin isEqualToString:@"YES"]) {
        
        [self.deleget PramisClickWhithRow:self.cellRow AndIslogin:YES];
        
        if (!sender.selected) {
            sender.selected = YES;
            [UIView animateWithDuration:0.5 animations:^{
                
                sender.imageView.transform = CGAffineTransformScale(sender.imageView.transform, 0.65, 0.65);
                
            } completion:^(BOOL finished) {
                
                
            }];
        }
        
    }else{
        
        [self.deleget PramisClickWhithRow:self.cellRow AndIslogin:NO];
    }
}

-(void)personSpaceClick{

    if ([self.deleget respondsToSelector:@selector(personSpaceClickWhithPersonId:)]) {
        
        [self.deleget personSpaceClickWhithPersonId:_model.m_id];
    }
}
@end